# 倒入模块
import pandas as pd
import matplotlib.pyplot as plt
import plotly as py
import plotly.graph_objs as go
import cufflinks as cf
from flask import Flask, render_template, request

app= Flask(__name__)

@app.route("/city")

def city_info():
	df = pd.read_csv ("hurun.csv", encoding = "utf8", sep="\t")
	df_北京=df[df['城市'].str.contains('北京')]
	df_上海=df[df['城市'].str.contains('上海')]
	df_广州=df[df['城市'].str.contains('广州')]
	df_深圳=df[df['城市'].str.contains('深圳')]
	### 数据统计计算
	df_北京各企业估值总和=df_北京['估值（亿人民币）'].sum()
	df_上海各企业估值总和=df_上海['估值（亿人民币）'].sum()
	df_广州各企业估值总和=df_广州['估值（亿人民币）'].sum()
	df_深圳各企业估值总和=df_深圳['估值（亿人民币）'].sum()
	df估值总和=pd.DataFrame({'北京各企业估值总和':df_北京['估值（亿人民币）'].sum(),'上海各企业估值总和':df_上海['估值（亿人民币）'].sum(),'广州各企业估值总和':df_广州['估值（亿人民币）'].sum(),'深圳各企业估值总和':df_深圳['估值（亿人民币）'].sum()},index=[0])
	df_北京各企业估值均值=df_北京['估值（亿人民币）'].mean()
	df_上海各企业估值均值=df_上海['估值（亿人民币）'].mean()
	df_广州各企业估值均值=df_广州['估值（亿人民币）'].mean()
	df_深圳各企业估值均值=df_深圳['估值（亿人民币）'].mean()
	df_北京行业估值=df_北京.groupby('行业').sum().drop(['成立年份','排名'],axis=1).reset_index()
	df_北京行业 = df_北京行业估值.sort_values(by='估值（亿人民币）',ascending=False).head(n=5)
	df_上海行业估值=df_上海.groupby('行业').sum().drop(['成立年份','排名'],axis=1).reset_index()
	df_上海行业 = df_上海行业估值.sort_values(by='估值（亿人民币）',ascending=False).head(n=5)
	df_广州行业估值=df_广州.groupby('行业').sum().drop(['成立年份','排名'],axis=1).reset_index()
	df_广州行业 = df_广州行业估值.sort_values(by='估值（亿人民币）',ascending=False).head(n=5)
	df_深圳行业估值=df_深圳.groupby('行业').sum().drop(['成立年份','排名'],axis=1).reset_index()
	df_深圳行业 = df_深圳行业估值.sort_values(by='估值（亿人民币）',ascending=False).head(n=5)
	df估值均值=pd.DataFrame({'北京各企业估值均值':df_北京['估值（亿人民币）'].mean(),'上海各企业估值均值':df_上海['估值（亿人民币）'].mean(),'广州各企业估值均值':df_广州['估值（亿人民币）'].mean(),'深圳各企业估值均值':df_深圳['估值（亿人民币）'].mean()},index=[0])
	df合并=pd.DataFrame({'北上广深企业估值':['北京各企业估值总和','上海各企业估值总和','广州各企业估值总和','深圳各企业估值总和'],'值':[df_北京['估值（亿人民币）'].sum(),df_上海['估值（亿人民币）'].sum(),df_广州['估值（亿人民币）'].sum(),df_深圳['估值（亿人民币）'].sum()]})
	df合并2=pd.DataFrame({'北上广深企业估值':['北京各企业估值均值','上海各企业估值均值','广州各企业估值均值','深圳各企业估值均值'],'值':[df_北京['估值（亿人民币）'].mean(),df_上海['估值（亿人民币）'].mean(),df_广州['估值（亿人民币）'].mean(),df_深圳['估值（亿人民币）'].mean()]})
	fig = df合并.iplot(kind="bar",x="北上广深企业估值",y="值",asFigure=True,title='北上广深估值总和对比')
	py.offline.plot(fig, filename="example.html",auto_open=False)
	# 读取html文件
	with open("example.html", encoding="utf8", mode="r") as f:   
		plot_all = "".join(f.readlines()) 
	fig = df合并2.iplot(kind="bar",color="blue",x="北上广深企业估值",y="值",asFigure=True,title='北上广深估值均值对比')
	py.offline.plot(fig, filename="example2.html",auto_open=False)
	# 读取html文件
	with open("example2.html", encoding="utf8", mode="r") as f:   
		plot_all2 = "".join(f.readlines()) 
	pyplt = py.offline.plot
	labels = df_北京行业['行业']
	values = df_北京行业['估值（亿人民币）']
	trace = [go.Pie(labels=labels, values=values)]
	layout = go.Layout(title = '北京前五个行业占比',)
	fig = go.Figure(data = trace, layout = layout)
	py.offline.plot(fig, filename="example北京.html",auto_open=False)
	with open("example北京.html", encoding="utf8", mode="r") as f:
	     plot_all3 = "".join(f.readlines())
	pyplt = py.offline.plot
	labels = df_上海行业['行业']
	values = df_上海行业['估值（亿人民币）']
	trace = [go.Pie(labels=labels, values=values)]
	layout = go.Layout(title = '上海前五个行业占比',)
	fig = go.Figure(data = trace, layout = layout)
	py.offline.plot(fig, filename="example上海.html",auto_open=False)
	with open("example上海.html", encoding="utf8", mode="r") as f:
     	 plot_all6 = "".join(f.readlines())
	pyplt = py.offline.plot
	labels = df_广州行业['行业']
	values = df_广州行业['估值（亿人民币）']
	trace = [go.Pie(labels=labels, values=values)]
	layout = go.Layout(title = '广州前五个行业占比',)
	fig = go.Figure(data = trace, layout = layout)
	py.offline.plot(fig, filename="example广州.html",auto_open=False)
	with open("example广州.html", encoding="utf8", mode="r") as f:
     	 plot_all5 = "".join(f.readlines())
	pyplt = py.offline.plot
	labels = df_深圳行业['行业']
	values = df_深圳行业['估值（亿人民币）']
	trace = [go.Pie(labels=labels, values=values)]
	layout = go.Layout(title = '深圳前五个行业占比',)
	fig = go.Figure(data = trace, layout = layout)
	py.offline.plot(fig, filename="example深圳.html",auto_open=False)
	with open("example深圳.html", encoding="utf8", mode="r") as f:
		 plot_all4 = "".join(f.readlines())
	return render_template("shujv.html",
		tu1=plot_all,tu2=plot_all2,biao1=df_北京.head().to_html(index=False),biao2=df_上海.head().to_html(index=False),biao3=df_广州.head().to_html(index=False),biao4=df_深圳.head().to_html(index=False),tu3=plot_all3,tu4=plot_all4,tu5=plot_all5,tu6=plot_all6)  

 
if __name__=='__main__':
 	app.run(
 		debug=True
 		)




